capture log close
clear
clear mata
clear matrix
set mem 500m
set more off
graph drop _all


#delimit ;


*	************************************************************************ *;
* 	File-Name: DynamicIndexValidity.do										 *;
*	Date:  04/03/13															 *;
*	Author: 	James Hollyer                                                *;
*	Purpose:   To examine the results of the preliminary Transparency IRT    *;
*	model and to assess index validity.										 *;
*	OS: Windows 7						 								 	 *;
*	************************************************************************ *;

use "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\HRV2013.dta";


*decode WDIdata_countryname, gen(country);
*decode WDIdata_countrycode, gen(wdicode);
*rename WDIdata_year year;

*replace year=year+1979;
*rename transparencyindexlb lower_bound;
*rename transparencyindexub upper_bound;

*drop WDIdata_*;

encode wdicode, gen(wdinum);

sort wdicode year;

gen inv_sd = 1/transparencyindexsd;

save "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FullWDITransparencyJAGS.dta",
replace;

*	************************************************************************ *;
*	The following will graph the index in a given year.  Graphs will be for  *;
*	1980.																	 *;
*	************************************************************************ *;

gsort year - transparencyindex;
gen countrycode=.;

forvalues i=1(1)155{;
replace countrycode=`i' if _n==`i';
local j = country[_n-1+`i'];
label define countrynumber `i' "`j'", add;
label values countrycode countrynumber;
};

*Figure 6;

graph twoway (rcap  lower_bound upper_bound countrycode if 
year==1980 & countrycode<26, horizontal ylabel(1(1)25, valuelabel angle(horizontal) 
labsize(small)) xlabel(-10(5)10)) (scatter countrycode transparencyindex if 
year==1980 & countrycode<26, xtitle(Transparency Index Value)
ytitle(Country) scheme(s2mono) graphregion(fcolor(white)) legend(off)
title(Transparency Index Values 1980) subtitle(High Transparency Countries) msize(tiny));

**graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\highscores1980.png",
width(1000) replace;


*Figure 5;

graph twoway (rcap  lower_bound upper_bound countrycode if 
year==1980 & countrycode>99, horizontal ylabel(100(1)125, valuelabel angle(horizontal) 
labsize(small)) xlabel(-10(5)10)) (scatter countrycode transparencyindex if 
year==1980 & countrycode>123, xtitle(Transparency Index Value)
ytitle(Country) scheme(s2mono) graphregion(fcolor(white)) legend(off)
title(Transparency Index Values 1980) subtitle(Low Transparency Countries) msize(tiny));

**graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\lowscores1980.png",
width(1000) replace;


*	************************************************************************ *;
*	The following will look at individual countries over time.  			 *;
*	************************************************************************ *;

sort wdicode year;

*Figure 7;

graph twoway (rcap lower_bound upper_bound year if wdicode=="VNM")
(scatter transparencyindex year if wdicode=="VNM", xtitle(Year) 
ytitle(Transparency Index Value) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Values) msize(small) name(VNM1));

graph twoway (rcap transdifflb transdiffub year if wdicode=="VNM")
(scatter transdiff year if wdicode=="VNM", xtitle(Year) 
ytitle(First Differences) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Changes) msize(small) name(VNM2));

graph combine VNM1 VNM2, scheme(s2mono) title(Vietnam);

**graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\vietnam.png",
width(1000) replace;

*Figure 10;

graph twoway (rcap lower_bound upper_bound year if wdicode=="ARG")
(scatter transparencyindex year if wdicode=="ARG", xtitle(Year) 
ytitle(Transparency Index Value) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Values) msize(small) name(ARG1));

graph twoway (rcap transdifflb transdiffub year if wdicode=="ARG")
(scatter transdiff year if wdicode=="ARG", xtitle(Year) 
ytitle(First Differences) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Changes) msize(small) name(ARG2));

graph combine ARG1 ARG2, scheme(s2mono) title(Argentina);

*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\argentina.png",
width(1000) replace;

*Figure 8;

graph twoway (rcap lower_bound upper_bound year if wdicode=="TZA")
(scatter transparencyindex year if wdicode=="TZA", xtitle(Year) 
ytitle(Transparency Index Value) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Values) msize(small) name(TZA1));

graph twoway (rcap transdifflb transdiffub year if wdicode=="TZA")
(scatter transdiff year if wdicode=="TZA", xtitle(Year) 
ytitle(First Differences) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Changes) msize(small) name(TZA2));

graph combine TZA1 TZA2, scheme(s2mono) title(Tanzania);


*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\tanzania.png",
width(1000) replace;


*Figure 9;

graph twoway (rcap lower_bound upper_bound year if wdicode=="SOM")
(scatter transparencyindex year if wdicode=="SOM", xtitle(Year) 
ytitle(Transparency Index Value) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Values) msize(small) name(SOM1));

graph twoway (rcap transdifflb transdiffub year if wdicode=="SOM")
(scatter transdiff year if wdicode=="SOM", xtitle(Year) 
ytitle(First Differences) scheme(s2mono) graphregion(fcolor(white)) 
legend(off) title(Transparency Index Changes) msize(small) name(SOM2));

graph combine SOM1 SOM2, scheme(s2mono) title(Somalia);

*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\somalia.png",
width(1000) replace;


*	************************************************************************ *;
*	The following will create time series plots of HRV scores for each 		 *;
*	country in the dataset.													 *;
*	************************************************************************ *;

*Appendix B;

graph drop _all;

replace country="The Bahamas" if country=="Bahamas, The";
replace country="Republic of Congo" if country=="Congo, Rep.";
replace country="Egypt" if country=="Egypt, Arab Rep.";
replace country="The Gambia" if country=="Gambia, The";
replace country="Iran" if country=="Iran, Islamic Rep.";
replace country="South Korea" if country=="Korea, Rep.";
replace country="St Lucia" if country=="St. Lucia";
replace country="North Korea" if country=="Korea, Dem. Rep.";
replace country="St Vincent and the Grenadines" if country=="St. Vincent and the Grenadines";
replace country="Venezuela" if country=="Venezuela, RB";
replace country="Yemen" if country=="Yemen, Rep.";
replace country="Democratic Republic of the Congo" if country=="Congo, Dem. Rep.";

local j=1;

forvalues i=1(31)4775{;
	local state = wdicode[_n-1+`i'];
	local name = country[_n-1+`i'];
	graph twoway (rcap lower_bound upper_bound year if wdicode=="`state'")
	(scatter transparencyindex year if wdicode=="`state'", xtitle(Year) 
	ytitle(HRV Value) scheme(s2mono) graphregion(fcolor(white)) 
	legend(off) title(HRV Values) subtitle(`name') msize(vtiny)
	name(g`j') ylabel(-15(10)15) nodraw);
	local j=`j'+1;
};

local j=1;
forvalues i=1(24)97{;
	local a1=`i'+1;
	local a2=`i'+2;
	local a3=`i'+3;
	local a4=`i'+4;
	local a5=`i'+5;
	local a6=`i'+6;
	local a7=`i'+7;
	local a8=`i'+8;
	local a9=`i'+9;
	local a10=`i'+10;
	local a11=`i'+11;
	local a12=`i'+12;
	local a13=`i'+13;
	local a14=`i'+14;
	local a15=`i'+15;
	local a16=`i'+16;
	local a17=`i'+17;
	local a18=`i'+18;
	local a19=`i'+19;
	local a20=`i'+20;
	local a21=`i'+21;
	local a22=`i'+22;
	local a23=`i'+23;

	graph combine g`i' g`a1' g`a2' g`a3' g`a4' g`a5' g`a6' g`a7' g`a8' g`a9' g`a10' g`a11' g`a12' 
	g`a13' g`a14' g`a15' g`a16' g`a17' g`a18' g`a19' g`a20' g`a21' g`a22' g`a23', 
	rows(6) cols(4) ycommon xcommon graphregion(fcolor(white)) xsize(7) ysize(10);
	
	*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\LotsofTimeSeries`j'.png",
	width(2000) replace;
	
	local j=`j'+1;
};

graph combine g121 g22 g123 g124 g125, rows(2) 
cols(4) ycommon xcommon graphregion(fcolor(white)) xsize(7) ysize(5);

*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\LotsofTimeSeries6.png",
width(2000) replace;

*	************************************************************************ *;
*	The following will compare the HRV values to the percentage of variable  *;
*	reported.																 *;
*	************************************************************************ *;

clear;

use "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\FullWDIIRTprep2.dta";

replace year=year+1979;

gen tot_reported=0;

forvalues i=1(1)240{;
	replace tot_reported=tot_reported+1 if var_`i'~=0;
	};

gen frac_reported= tot_reported/240;

keep countryname countrycode year frac_reported;

rename countrycode wdicode;

sort wdicode year;

merge 1:1 wdicode year using "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FullWDITransparencyJAGS.dta";

sum frac_reported;

replace frac_reported = (frac_reported-r(mean))/r(sd);

sum transparencyindex;

replace transparencyindex = (transparencyindex-r(mean))/r(sd);

*Figure 2;

graph twoway (scatter frac_reported transparencyindex, 
graphregion(fcolor(white)) xtitle(HRV Transparency Index) 
ytitle(Fraction Reported) msize(tiny) scheme(s2mono) 
title(Fraction Reported against HRV Index)) (function y=x, range(-5 3) legend(off))
(lowess frac_reported transparencyindex, mlabel(wdicode) mlabposition(12) 
mlabsize(medsmall) msize(tiny));

*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\HRVvFracReported.png",
width(1000) replace;

corr frac_reported transparencyindex;


*	************************************************************************ *;
*	The following will compare our measure with Freedom House's Freedom of   *;
*	the Press measure.														 *;
*	************************************************************************ *;

clear;

insheet using "c:\users\james\desktop\my dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FHScores2013.csv",
comma names;

drop if wdicode=="";

reshape long score, i(wdicode) j(year);

rename score FHpressscore;

drop v*;

sort wdicode year;

merge 1:1 wdicode year using "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FullWDITransparencyJAGS.dta";

*Those FH observations lacking a match are primarily from E Eur. and a variety of countries;
*that legitimately should not be in the data (due to issues of rectangularity with the HRV scores).;
*Those in the HRV data that lack a match are primiarly a rsult of the greater coverage of HRV than;
*FH.;

drop if _merge==1;
drop _merge;

*I invert the FH index so that high values correspond to more freedom.;

replace FHpressscore = abs(FHpressscore-100);

*And I standardize both our measure and Freedom House's;

sum FHpressscore;

replace FHpressscore = (FHpressscore - r(mean))/r(sd);

gen norm_transparencyindex =.;

sum transparencyindex;

replace norm_transparencyindex = (transparencyindex - r(mean))/r(sd);

save "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FH_v_HRV.dta",
replace;

clear;

use "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\dd.dta";

sort wdicode year;

merge 1:1 wdicode year using "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FH_v_HRV.dta";

*There are some observations in the DD data that are not in HRV after 1980.  I drop these.;
*The DD data only go through 2008.  I keep extra HRV observations.;

drop if _merge==1;
drop _merge;

sort wdicode year;

save "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FH_v_HRV2.dta",
replace;

clear;

insheet using "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\PWT71full.txt", comma names;

rename countryisocode wdicode;

drop if year<1980;

sort wdicode year;

merge 1:1 wdicode year using "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\FH_v_HRV2.dta";

*The PWT doesn't run throgh 2010, so a number of HRV observations remain unmatched.;
*A number of PWT observations are also unmatched -- mostly countries that recently;
*came into existence.  The exception here is Germany, which we should drop from the IRT;
*given the issues with measurement from the pre-unification period in the WDI's coding scheme.;

drop if _merge==1;

drop _merge;

*	************************************************************************ *;
*	The following will attempt to address concerns about the confounding     *;
*	role of capacity by looking at the relationship between HRV scores and   *;
*	GDP per capita in democracies and autocracies.							 *;
*	************************************************************************ *;

gen lngdpcap = ln(rgdpch);

*Figure 1;

graph twoway 
(scatter norm_transparencyindex lngdpcap if democracy==1, msymbol(o) msize(vsmall) mcolor(gs13))
(scatter norm_transparencyindex lngdpcap if democracy==0, msymbol(+) msize(vsmall) mcolor(gs10))
(lowess norm_transparencyindex lngdpcap if democracy==1, lpattern(dash))
(lowess norm_transparencyindex lngdpcap if democracy==0, lpattern(solid) graphregion(fcolor(white)) 
xtitle(Log GDP per Capita) ytitle(HRV Score) title(HRV Scores v. Log GDP per Capita)
subtitle(In Democracies and Autocracies) scheme(s2mono)
legend(lab(1 "Fitted Democracies") lab(2 "Fitted Autocracies") lab(3 "Obs. Democracies") lab(4 "Obs. Autocracies")));

*graph export "c:\users\james\desktop\dropbox\transparency_and_democracy\transparencyindex\PAReplicationMaterials\Index Properties\capacity.png",
width(1000) replace;





